package org.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

/*The sells books activity class is called when the user wishes to sell his/her book using app.
 * This class allows user to scan the book using camera and then by scanning the ISBN
 * of the books sends request to the remote database for its related information
 * Once all the information is received the user can quoate the price and submit it to our
 * database
 * */
public class SellBooksActivity extends Activity {
	public String contents = "abc";
	@SuppressWarnings("unused")
	private TextView textview1, isbntext;
	private String Title, Title_long, Author1, Publisher;
	private String ID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle extras = getIntent().getExtras();

		if (extras == null) {
			return;
		}
		ID = (String) extras.getSerializable("ID");
		System.out.println("**************This activity starts*********");
		System.out.println("ID came is---> " + ID);

		setContentView(R.layout.sellbookslayout);
		textview1 = (TextView) findViewById(R.id.textview1);

	}
	
	//the onclick method that starts the camera for scanning the book and 
	//retreiving the book information
	public void onClick(View view) {

		Intent intent = new Intent("com.google.zxing.client.android.SCAN");
		intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE",
				"QR_CODE_MODE");
		startActivityForResult(intent, 0);
		contents = "gfds";
	}
	
	
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		System.out.println("***********1");
		if (requestCode == 0) {

			if (resultCode == RESULT_OK) {

				contents = intent.getStringExtra("SCAN_RESULT");

				try {
					getPage(contents);
				} catch (Exception e) {

					e.printStackTrace();
				}

			} else if (resultCode == RESULT_CANCELED) {

			}
		}
	}

	public void onClick1(View view) {
		isbntext.setText(" Isbn :  " + contents);
		System.out.println("contnts2 " + contents);
	}

	//This method is called when scanning is completed and ISBN is 
	//required to send to the remote database to get all its information
	private void getPage(String contents) throws Exception {
		System.out.println("Contents came: " + contents);
		String line = "", s = "";
		String url = "http://isbndb.com/api/books.xml?access_key=GQIKRCH3&results=texts+prices&index1=isbn&value1="
				+ contents;
		URL yahoo = new URL(url);
		URLConnection yc = yahoo.openConnection();
		BufferedReader in = new BufferedReader(new InputStreamReader(yc
				.getInputStream()));

		while ((line = in.readLine()) != null) {
			s += line;
		}

		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			InputSource is = new InputSource();
			is.setCharacterStream(new StringReader(s));

			Document doc = db.parse(is);
			NodeList nodes = doc.getElementsByTagName("BookData");

			for (int i = 0; i < nodes.getLength(); i++) {
				Element element = (Element) nodes.item(i);

				NodeList title = element.getElementsByTagName("Title");
				Element line1 = (Element) title.item(0);
				System.out.println("Title: "
						+ getCharacterDataFromElement(line1));
				Title = getCharacterDataFromElement(line1);

				NodeList title_long = element.getElementsByTagName("TitleLong");
				line1 = (Element) title_long.item(0);
				System.out.println("Title Long: "
						+ getCharacterDataFromElement(line1));
				Title_long = getCharacterDataFromElement(line1);

				NodeList Author = element.getElementsByTagName("AuthorsText");
				line1 = (Element) Author.item(0);
				System.out.println("Authors Text: "
						+ getCharacterDataFromElement(line1));
				Author1 = getCharacterDataFromElement(line1);

				NodeList PublisherText = element
						.getElementsByTagName("PublisherText");
				line1 = (Element) PublisherText.item(0);
				System.out.println("Publisher Text: "
						+ getCharacterDataFromElement(line1));
				Publisher = getCharacterDataFromElement(line1);

				NodeList Summary1 = element.getElementsByTagName("Summary");
				line1 = (Element) Summary1.item(0);
				getCharacterDataFromElement(line1);

				NodeList Notes1 = element.getElementsByTagName("Notes");
				line1 = (Element) Notes1.item(0);
				getCharacterDataFromElement(line1);

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		Intent i = new Intent(this, AddStudentDetailsActivity.class);
		i.putExtra("Value1", "This value one for ActivityTwo ");
		i.putExtra("Title", Title);
		i.putExtra("Title_long", Title_long);
		i.putExtra("Author1", Author1);
		i.putExtra("Publisher", Publisher);
		i.putExtra("isbn", contents);
		i.putExtra("ID", ID);

		startActivity(i);
		return;

	}
	//The logout functionality
	public void logout(View view) throws Exception {
		Intent i = new Intent(this, NewOrExisitingUserActivity.class);
		startActivity(i);
	}
	
	public static String getCharacterDataFromElement(Element e) {
		Node child = e.getFirstChild();
		if (child instanceof CharacterData) {
			CharacterData cd = (CharacterData) child;
			return cd.getData();
		}
		return "NOT FOUND";
	}
}
